package org.example.mapper;

import org.example.model.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StudentMapper {
    @Insert("INSERT INTO student (name, class_name, age, gender, create_time, update_time) VALUES (#{name}, #{className}, #{age}, #{gender}, now(), now())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertStudent(Student student);

    @Update("UPDATE student SET name = #{name}, class_name = #{className}, age = #{age}, gender = #{gender}, update_time = now() WHERE id = #{id}")
    int updateStudent(Student student);

    @Delete("DELETE FROM student WHERE id = #{id}")
    int deleteStudent(Long id);

    @Select("SELECT * FROM student WHERE id = #{id}")
    Student getStudentById(Long id);

    @Select("SELECT * FROM student")
    List<Student> getAllStudents();

    @Select("SELECT * FROM student WHERE class_name = #{className}")
    List<Student> getStudentsByClass(String className);

    @Insert("<script>INSERT INTO student (name, class_name, age, gender, create_time, update_time) VALUES " +
            "<foreach collection='students' item='student' separator=','>" +
            "(#{student.name}, #{student.className}, #{student.age}, #{student.gender}, now(), now())" +
            "</foreach></script>")
    void batchInsertStudents(List<Student> students);
}